package common;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author jack.wu
 * @since 2019/9/11
 */
public class CollectorTest {

    static class User {
        private Long id;
        private String name;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public User(Long id, String name) {
            this.id = id;
            this.name = name;
        }
    }

    public static void main(String[] args) {
//        // 对集合每个数*10
////        List<Integer> ints = Arrays.asList(1, 2, 3);
////        List<Integer> list = ints.stream().map(i -> i * 10).collect(Collectors.toList());
////        System.out.println(list);
////        // LIST->MAP
////        List<User> users = Arrays.asList(new User(1L, "张三"), new User(2L, "李四"));
////        Map<Long, String> map = users.stream().collect(Collectors.toMap(User::getId, User::getName));
////        System.out.println(map);
////        // 元素拼接
////        List<String> list1 = Arrays.asList("1", "2", "3");
////        String result = list1.stream().collect(Collectors.joining(","));
////        Map<String,String> map1 = new HashMap<>(1);
////        map1.put("1","1");
////        map1.put("1","2");
////        map1.put("2","2");
////        map1.put("3","3");
////        System.out.println(map1);
//        Stream<Integer> integerStream = Stream.of(1, 2, 3).map(i->i*10);
//        System.out.println(integerStream.max(new Comparator<Integer>() {
//            @Override
//            public int compare(Integer o1, Integer o2) {
//                return o1.compareTo(o2);
//            }
//        }).get());
//        collectingAndThen();
//        // concat
//        Stream.concat(Stream.of("1", "2"), Stream.of("3")).forEach(System.out::println);
//        // distinct
//        Stream.of("1", "2", "1").distinct().forEach(System.out::println);
//        // filter
//        Stream.of("1", "2", "3").filter("3"::equals).forEach(System.out::print);
//        // map
//        Stream.of("a", "b", "c").map(String::toUpperCase).forEach(System.out::print);
        reducing();

    }

    public static void collectingAndThen() {
        List<String> list = Arrays.asList("a", "b", "c");
        String collect = String.join(",", list);
        System.out.println(collect);
        String collect1 = Stream.of("a", "b", "c").collect(Collectors.collectingAndThen(Collectors.joining(","), x -> x + "a"));
        System.out.println(collect1);
    }

    public static void reducing() {
        Integer reduce = Stream.of(1, 2, 3).map(x -> x + 1).reduce(0, Integer::sum);
        System.out.println(reduce);
    }
}
